package com.openharmony.filepicker.utils;

import com.openharmony.filepicker.bean.FileModel;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.data.resultset.ResultSet;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static List<FileModel> getImagesData(Context context) {

        List<FileModel> resultList = new ArrayList<>();

        DataAbilityHelper helper = DataAbilityHelper.creator(context);

        String[] projection = new String[]{
                AVStorage.Images.Media.ID,
                AVStorage.Images.Media.TITLE,
                AVStorage.Images.Media.SIZE,
                AVStorage.Images.Media.MIME_TYPE,
                AVStorage.Images.Media.VOLUME_NAME
        };

        try {
            ResultSet result = helper
                    .query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,
                    projection,
                    null);

            if (result == null) {
                return resultList;
            }

            while (result.goToNextRow()) {
                int id = result.getInt(result.getColumnIndexForName(AVStorage.Images.Media.ID));
                String name = result.getString(result.getColumnIndexForName(AVStorage.Images.Media.VOLUME_NAME));
                int type = result.getInt(result.getColumnIndexForName(AVStorage.Images.Media.MIME_TYPE));
                String title = result.getString(result.getColumnIndexForName(AVStorage.Images.Media.TITLE));
                Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, String.valueOf(id));
                LogUtils.e("FileUtils", "uri : " + uri.toString() + ", type : " + type + ", title : " + title);

                FileModel fileModel = new FileModel();
                fileModel.setFileName(title);
                fileModel.setType(type);

                resultList.add(fileModel);
            }

            result.close();

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }

        return resultList;
    }

    public static List<FileModel> getVideoData(Context context) {
        List<FileModel> resultList = new ArrayList<>();

        DataAbilityHelper helper = DataAbilityHelper.creator(context);

        String[] projection = new String[]{
                AVStorage.Video.Media.ID,
                AVStorage.Video.Media.TITLE,
                AVStorage.Video.Media.SIZE,
                AVStorage.Video.Media.MIME_TYPE,
                AVStorage.Video.Media.VOLUME_NAME
        };

        try {
            ResultSet result = helper
                    .query(AVStorage.Video.Media.EXTERNAL_DATA_ABILITY_URI, projection, null);

            if (result == null) {
                return resultList;
            }

            while (result.goToNextRow()) {
                int id = result.getInt(result.getColumnIndexForName(AVStorage.Video.Media.ID));
                String name = result.getString(result.getColumnIndexForName(AVStorage.Video.Media.VOLUME_NAME));
                int type = result.getInt(result.getColumnIndexForName(AVStorage.Video.Media.MIME_TYPE));
                String title = result.getString(result.getColumnIndexForName(AVStorage.Video.Media.TITLE));
                Uri uri = Uri.appendEncodedPathToUri(AVStorage.Video.Media.EXTERNAL_DATA_ABILITY_URI, String.valueOf(id));
                LogUtils.e("FileUtils", "uri : " + uri.toString() + ", type : " + type + ", title : " + title);

                FileModel fileModel = new FileModel();
                fileModel.setFileName(title);
                fileModel.setType(type);

                resultList.add(fileModel);
            }

            result.close();

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }

        return resultList;
    }
}
